package com.example.bysj_sb.dao;


import com.example.bysj_sb.domain.User;
import com.example.bysj_sb.domain.authority.Actor;
import com.example.bysj_sb.domain.authority.Role;
import com.example.bysj_sb.service.ActorService;
import com.example.bysj_sb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Repository("userDao")
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ActorService actorService;
    @Autowired
    private RoleService roleService;
    //增加
    public boolean add(User user) {
        String sql = "INSERT INTO User (username,password,actor_id)" +
                "VALUES(?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, user.getUsername(),
                user.getPassword(),user.getActor().getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    //通过定义内部类来返回查询结果
    public class UserRowMapper implements RowMapper<User>{
        @Override
        public User mapRow(ResultSet resultSet, int i) throws SQLException {

            int id=resultSet.getInt("id");
            String username=resultSet.getString("username");
            String password=resultSet.getString("password");
            Role role =roleService.find(resultSet.getInt("role_id"));
            Actor actor =actorService.find(resultSet.getInt("actor_id"));
            //时间戳对象
            Timestamp timestamp = resultSet.getTimestamp("lastLoginTime");
            LocalDateTime lastLoginTime = null;
            if (timestamp != null) {
                lastLoginTime = timestamp.toLocalDateTime();
            }
            User user = new User();
            user.setId(id);
            user.setUsername(username);
            user.setPassword(password);
            user.setRole(role);
            user.setActor(actor);
            user.setLoginTime(lastLoginTime);
            return user;
        }
    }
   //根据id查询
    public User find(int id){
        String sql = "SELECT * FROM User WHERE id = ?";
        List<User> users = jdbcTemplate.query(sql, new Object[]{id}, new UserRowMapper());
        if(!users.isEmpty()){
            return users.get(0);
        } else
            return null;
    }
    //查询全部
    public List<User> findAll(){
        String sql = "SELECT * FROM User ";
        List<User> Users = jdbcTemplate.query(sql, new UserRowMapper());
        return Users;
    }
    //修改
    public boolean update(User user){
        String sql = "UPDATE User SET username = ?,password = ?" + "WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    //根据id删除
    public boolean delete(int id){
        String sql = "DELETE FROM User WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    //根据username查询
    public User findByName(String name){
        String sql = "SELECT * FROM User WHERE username = ?";
        List<User> users = jdbcTemplate.query(sql, new Object[]{name}, new UserRowMapper());
        if(!users.isEmpty()){
            return users.get(0);
        } else
            return null;
    }
    //登录方法，根据user对象返回actor对象
    public Actor login(User user){
        String sql = "SELECT * FROM user WHERE username =? AND password=?";
        List<Actor> actors = jdbcTemplate.query(sql,
                new Object[]{user.getUsername(),user.getPassword()}, new RowMapper(){
            //定义匿名类
                    @Override
                    public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                        int actor_id =resultSet.getInt("actor_id");
                        Actor actor =actorService.find(actor_id);
                        return actor;
                    }
                });

        if(!actors.isEmpty()){
            return actors.get(0);
        } else
            return null;
    }

}


